package controller;

import bean.dto.Dto;
import bean.pojo.GdArticle;
import bean.pojo.GdCategory;
import bean.pojo.GdUser;
import bean.pojo.GdUserArticlelist;
import bean.vo.ArticleVo;
import bean.vo.CategoryVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import service.GdArticleService;
import service.GdCategoryService;
import service.GdUserArticlelistService;
import service.TokenService;
import utils.DtoUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private TokenService tokenService;
    @Resource
    private GdArticleService gdArticleService;
    @Resource
    private GdCategoryService gdCategoryService;
    @Resource
    private GdUserArticlelistService gdUserArticlelistService;
    /**
     * 查询所有标签
     */
    @RequestMapping(value = "/allCategory",method = RequestMethod.GET)
    public @ResponseBody Dto allCategory(HttpServletRequest request){
        //String token = request.getHeader("token");
        try {
            GdCategory gdCategory = new GdCategory();
            //if (tokenService.validate(request.getHeader("user-agent"),token)){
                List<GdCategory> categoryList = gdCategoryService.select();
                if (categoryList!=null){
                    return DtoUtil.returnSuccessWithData("查询成功",categoryList);
                }
                return DtoUtil.returnFail("error","查询失败");
            //}
            //return DtoUtil.returnFail("error","token失效");
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("error","查询失败");
        }
    }

    /**
     * 通过标签查找文章
     * @param categoryId
     * @param request
     * @return
     */
    @RequestMapping(value = "/searchByCategory",method = RequestMethod.GET)
    public @ResponseBody
    Dto searchByCategory(@RequestParam("categoryId") long categoryId,
//                         @RequestParam("token") String token,
                         HttpServletRequest request){
//        String token = request.getHeader("token");
        try {
//            if (tokenService.validate(request.getHeader("user-agent"),token)){
                GdCategory category = gdCategoryService.selectByCategoryId(categoryId);
                List<GdCategory> categoryList = gdCategoryService.selectAllBase();
                boolean isSecondLevel = false;
                List<GdArticle> articleList = null;
                for (int i = 0; i < categoryList.size(); i++) {
                    if (categoryList.contains(category.getParentCategory())){
                        isSecondLevel=true;
                        break;
                    }
                }
                long a = category.getParentCategory();
                Long b = a;
                if (b==null||b==0){//如果==null传入的是一级id
                    articleList = gdArticleService.selectByCategory("classify1",categoryId);
                }else if(isSecondLevel){//判断是否是2级
                    articleList = gdArticleService.selectByCategory("classify2",categoryId);
                }else{//传入的是三级标签
                    articleList = gdArticleService.selectByCategory("classify3",categoryId);
                }
                if (articleList!=null)
                    return DtoUtil.returnSuccessWithData("查询成功",articleList);
                return DtoUtil.returnSuccessWithData("查询结束","此标签下未查询到文章");
//            }
//            return DtoUtil.returnFail("error","token失效");
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("error","查询失败");
        }
    }

    /**
     * 阅读量++
     * @param articleVo
     * @param request
     * @return
     */
    @RequestMapping(value = "/viewNumCount",method = RequestMethod.POST)
    public @ResponseBody Dto viewNumCount(@RequestBody ArticleVo articleVo,
                             HttpServletRequest request){
        String token = request.getHeader("token");
        try {
            if (gdArticleService.updateViewNum(articleVo.getArticleId()))
                return DtoUtil.returnSuccessWithMsg("阅读量更新成功");
            return DtoUtil.returnFail("error","更新失败");
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("error","更新失败");
        }
    }

    /**
     * 根据文章id显示文章
     * @param articleVo
     * @param request
     * @return
     */
    @RequestMapping(value = "/showArticle",method = RequestMethod.POST)
    public @ResponseBody Dto showArticle(@RequestBody ArticleVo articleVo,
                                         @RequestParam int userId,
                                         HttpServletRequest request){
//        String token = request.getHeader("token");
        try {
//            GdUser user = tokenService.getUser(token);
            GdUserArticlelist gdUserArticlelist = new GdUserArticlelist();
            gdUserArticlelist.setArticle(articleVo.getArticleId());
//            gdUserArticlelist.setUserId(user.getId());
            gdUserArticlelist.setUserId(userId);
//            if (tokenService.validate(request.getHeader("user-agent"),token)){
                //根据返回值在页面显示收藏/取消收藏
                if (gdArticleService.isFavorite(gdUserArticlelist)){
                    return DtoUtil.returnSuccessWithData("收藏状态",1);
                }
                return DtoUtil.returnSuccessWithData("收藏状态",0);
//            }
//            return DtoUtil.returnFail("error","token已失效");
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("error","显示失败");
        }
    }

    /**
     * 收藏/取消收藏文章
     * @param articleVo
     * @param request
     * @return
     */
    @RequestMapping(value = "/favorite",method = RequestMethod.POST)
    public @ResponseBody Dto addFavorite(@RequestBody ArticleVo articleVo,
                                         HttpServletRequest request){
        String token = request.getHeader("token");
        try {
            GdUser user = tokenService.getUser(token);
            if (tokenService.validate(request.getHeader("user-agent"),token)){
                if (gdUserArticlelistService.isFavorite(articleVo.getArticleId(),user.getId())){//判断是否已收藏
                    if (gdUserArticlelistService.delete(user.getId(),articleVo.getArticleId())){
                        return DtoUtil.returnSuccessWithMsg("取消收藏成功");
                    }
                    return DtoUtil.returnFail("error","取消收藏失败");
                }
                if (gdUserArticlelistService.insert(user.getId(),articleVo.getArticleId())){
                    return DtoUtil.returnSuccessWithMsg("收藏成功");
                }
                return DtoUtil.returnFail("error","取消收藏失败");
            }
            return DtoUtil.returnFail("error","token失效");
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("error","添加/取消收藏失败");
        }
    }
}
